import { Body, Controller, Delete, Get, Param, Post, Query } from '@nestjs/common';
import { MiniappArticleService } from './miniapp-article.service';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
import { AuthTenant } from '~/modules/tenant/tenant.decorator';
import { ArticleContentEntity } from '~/modules/article/article-content/article-content.entity';
import { ArticleCategoryEntity } from '~/modules/article/article-category/article-category.entity';
import { Public } from '~/modules/auth/decorators/public.decorator';
import { ApiTenantHeader } from '~/common/decorators/api-header-tenant.decorator';
import { ModuleSwaggerTags } from '~/constants/modules.constant';
import { AuthUser } from '~/modules/auth/decorators/auth-user.decorator';
import { ArticleContentDetailEntity } from './miniapp-article.model';
import { ArticleContentCollectEntity } from '~/modules/article/article-content/article-content-collect/aritcle-content-collect.entity';
import { PagerDto } from '~/common/dto/pager.dto';
import { Pagination } from '~/helper/paginate/pagination';
import { AllowAnon } from '~/modules/auth/decorators/allow-anon.decorator';
import { ArticleContentService } from '~/modules/article/article-content/article-content.service';
import { ArticleContentUserReviewService } from '~/modules/article/article-content/article-content-user-review/article-content-user-review.service';
import { ArticleContentUserReviewDto } from '~/modules/article/article-content/article-content-user-review/article-content-user-review.dto';

@ApiBearerAuth()
@ApiTags(ModuleSwaggerTags.ARTICLE)
@Controller('article')
export class MiniappArticleController {
  constructor(
    private readonly miniappArticleService: MiniappArticleService,
    private readonly articleContentService: ArticleContentService,
    private readonly articleContentUserReviewService: ArticleContentUserReviewService
  ) {}

  // 推荐文章
  @Public()
  @Get('recommend')
  @ApiTenantHeader()
  @ApiOperation({ summary: '获取推荐文章' })
  async getRecommend(@AuthTenant() tenant: IAuthTenant): Promise<ArticleContentEntity[]> {
    return await this.miniappArticleService.findRecommend(tenant);
  }

  @Public()
  @Get('detail/:id')
  @ApiOperation({ summary: '获取文章详情' })
  async getDetail(
    @Param('id') id: number,
    @AuthUser() user: IAuthUser
  ): Promise<Partial<ArticleContentDetailEntity>> {
    return await this.miniappArticleService.findOne(id, user);
  }

  @Public()
  @Get('category')
  @ApiTenantHeader()
  @ApiOperation({ summary: '获取文章分类' })
  async getCategory(@AuthTenant() tenant: IAuthTenant): Promise<ArticleCategoryEntity[]> {
    return await this.miniappArticleService.findAllByTenantId(tenant);
  }

  // 根据分类查文章
  @Public()
  @Get('category/:id')
  @ApiTenantHeader()
  @ApiOperation({ summary: '获取分类下的文章' })
  async getArticleByCategory(
    @Query('page') dto: PagerDto,
    @Param('id') id: number,
    @AuthTenant() tenant: IAuthTenant
  ): Promise<Pagination<ArticleContentEntity>> {
    return await this.miniappArticleService.findListByCategoryId({
      ...dto,
      categoryId: id,
      tenantId: tenant.id,
    });
  }

  @Public()
  @Get('query/name')
  @ApiTenantHeader()
  @ApiOperation({ summary: '根据名称查询文章' })
  async getArticleByName(@Query('name') name: string, @AuthTenant() tenant: IAuthTenant) {
    return await this.miniappArticleService.findByName(name, tenant.id);
  }

  @Post('like/:id')
  @ApiOperation({ summary: '点赞' })
  @ApiTenantHeader()
  async likeArticle(@Param('id') id: number, @AuthUser() user: IAuthUser): Promise<void> {
    await this.miniappArticleService.like(id, user);
  }

  @Delete('like/:id')
  @ApiOperation({ summary: '取消点赞' })
  async unlikeArticle(@Param('id') id: number, @AuthUser() user: IAuthUser): Promise<void> {
    await this.miniappArticleService.unlike(id, user);
  }

  @AllowAnon()
  @Post('collect/:id')
  @ApiOperation({ summary: '收藏' })
  async collectArticle(@Param('id') id: number, @AuthUser() user: IAuthUser): Promise<void> {
    await this.miniappArticleService.collect(id, user);
  }

  @AllowAnon()
  @Delete('collect/:id')
  @ApiOperation({ summary: '取消收藏' })
  async uncollectArticle(@Param('id') id: number, @AuthUser() user: IAuthUser): Promise<void> {
    await this.miniappArticleService.uncollect(id, user);
  }

  @AllowAnon()
  @Get('collect/self')
  @ApiOperation({ summary: '获取我的收藏' })
  async getCollect(@AuthUser() user: IAuthUser): Promise<Array<Partial<ArticleContentEntity>>> {
    return await this.miniappArticleService.getCollectionByUser(user.uid);
  }

	@AllowAnon()
  @Post('share/:id')
  @ApiOperation({ summary: '分享文章' })
  async shareArticle(@Param('id') id: number, @AuthUser() user: IAuthUser): Promise<void> {
    await this.miniappArticleService.share(id, user?.uid ?? null);
  }

  @Public()
  @Get('recommend/top')
  @ApiTenantHeader()
  @ApiOperation({ summary: '获取置顶推荐文章' })
  async getTopRecommend(@Query('categoryId') categoryId: number): Promise<ArticleContentEntity[]> {
    return await this.articleContentService.findTopRecommendByScience(categoryId);
  }

	@AllowAnon()
  @Post('review/:id')
  @ApiOperation({ summary: '用户评论文章' })
  @ApiBody({ type: ArticleContentUserReviewDto })
  async reviewArticle(
    @Param('id') id: number,
    @Body() dto: ArticleContentUserReviewDto,
    @AuthUser() user: IAuthUser
  ) {
    await this.articleContentUserReviewService.save(user.uid, id, dto);
  }
}
